package me.zhengjie.modules.other.rest;

import com.google.common.collect.Lists;
import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.common.dto.DataRequest;
import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.common.util.RedisKeyUtil;
import com.qiaofang.applet.common.util.RedisUtil;
import com.qiaofang.applet.constants.DictConstants;
import com.qiaofang.applet.service.other.domain.Corporation;
import com.qiaofang.applet.service.other.domain.DictCorp;
import com.qiaofang.applet.service.other.service.CorporationService;
import com.qiaofang.applet.service.other.service.DictCorpService;
import com.qiaofang.applet.service.other.service.dto.DictDto;
import com.qiaofang.applet.service.other.service.dto.DictTypeDto;
import com.qiaofang.applet.service.other.service.mapstruct.DictCorpMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by IDEA.
 *
 * @author QF2019
 * @company qiaofang
 * @date 2020/9/12 11:35
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "内容：UI管理")
@RequestMapping("/api/dictCorp")
public class DictCorpController {

    @Autowired
    private DictCorpService dictCorpService;

    @Autowired
    private DictCorpMapper dictCorpMapper;

    @Autowired
    private CorporationService corporationService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping
    @ApiOperation(value = "保存字典数据", notes = "保存字典数据")
    public ResponseEntity<Object> save(@RequestBody List<DictDto> dictDtos) {
        dictCorpService.saveDictCorp(dictCorpMapper.toEntity(dictDtos));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping
    @ApiOperation(value = "查询移动端UI列表", notes = "查询移动端UI列表")
    public ResponseEntity<Map<String, List<DictDto>>> list() {
        String corporationCode = SecurityUtils.getCorporationCode();
        //管理后台UI展示
        String strArr[] = {DictConstants.SUBJECT_COLOR.getType(), DictConstants.INDEX_TOP_SHOW.getType(), DictConstants.SLIDE_SHOW.getType(),
                DictConstants.BOX_UTIL.getType(), DictConstants.FIXED_TOPIC_NAME.getType(), DictConstants.FY_RECOMMEND_SHOW.getType()
                , DictConstants.OTHER_LOGO.getType(), DictConstants.INDEX_PAGE.getType()};
        Set<String> dictTypes = new HashSet<>(Arrays.asList(strArr));
        return ResponseEntity.ok(dictCorpService.getDictCollectionByType(dictTypes, corporationCode));
    }

    @GetMapping("/initDict")
    @ApiOperation(value = "恢复默认字典数据", notes = "恢复默认字典数据")
    public ResponseEntity<Object> initDict(@RequestParam("corporationCode") String corporationCode) {
        Corporation corporation = new Corporation();
        corporation.setCorporationCode(corporationCode);
        if (corporationService.findOne(Example.of(corporation)).isPresent()){
            dictCorpService.removeCorporationData(corporationCode);
            dictCorpService.initCorporationData(corporationCode);
        }
        return ResponseEntity.ok(null);
    }

    @GetMapping("/initDictByType")
    @ApiOperation(value = "按类型恢复默认字典数据", notes = "按类型恢复默认字典数据")
    public ResponseEntity<Object> initDictByType(@RequestParam("dictType") String dictType, @RequestParam("corporationCode") String corporationCode) {
        Corporation corporation = new Corporation();
        corporation.setCorporationCode(corporationCode);
        if (corporationService.findOne(Example.of(corporation)).isPresent()){
            dictCorpService.removeCorporationDataByDictType(corporation.getCorporationCode(), dictType);
            dictCorpService.initCorporationDataByDictType(corporation.getCorporationCode(), dictType);
        }
        return ResponseEntity.ok(null);
    }

    @PostMapping("/getDictList")
    @ApiOperation(value = "字典列表接口", notes = "字典列表接口")
    public ResponseEntity<Map<String, List<DictDto>>> getDictList(@RequestBody DictTypeDto dictTypeDto) {
        Map<String, List<DictDto>> result = dictCorpService.getDictCollectionByType(dictTypeDto.getDictTypeSet(), SecurityUtils.getCorporationCode());
        return ResponseEntity.ok(result);
    }
}
